<?php
/**
 * Created by PhpStorm.
 * User: xiong <1099446868@qq.com>
 * DateTime: 2018/11/2
 * TIME: 22:32
 */

namespace app\index\model;


use think\facade\Cookie;
use think\facade\Session;

class User extends BaseModel {

    /** 验证登录字段格式是否正确
     * @param $data
     * @return bool|string
     */
    public static function phone_email_login($data) {
        if (preg_match('/^1[3-9][0-9]\d{8}$/', $data)) {
            return 'phone';

        } else if (filter_var($data, FILTER_VALIDATE_EMAIL)) {
            return 'email';
        } else {
            return false;
        }
    }

    /**
     * @param $data 登录数据
     * @param $phone  phone/email
     * @return array
     */
    public static function login($data, $phone) {

        $user = self::where($phone, $data['phone_email'])->find();
        $login = [
            'code' => 400
        ];
        if($user == null){
            return $login;
        }
        if ($user['password'] == md5($data['password'])) {
            $login['code'] = 200;
            Cookie::set('username', $user['username'], 3600);
            Cookie::set($phone, $user[$phone], 3600);
            Session::set('username', $user['username']);
            Session::set($phone, $user[$phone]);
            return $login;
        } else {
            $login['res'] = '账号密码错误';
            return $login;
        }
    }

    /**
     * @param $name 查username字段
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public static function getUserByName($name) {
        $user = self::where('username', $name)->find();
        return $user;
    }

    /** 验证注册参数phone_email
     * @param $data
     * @return bool|string  false代表参数既不是phone，也不是email
     *                      string代表存在性
     */
    public static function phone_email($data) {
        if (preg_match('/^1[3-9][0-9]\d{8}$/', $data)) {
            if (self::field_exists('phone', $data)) {
                return 'phone_exist';   //代表存在
            } else {
                return 'phone';   //代表bu存在
            }

        } else if (filter_var($data, FILTER_VALIDATE_EMAIL)) {
            if (self::field_exists('email', $data)) {
                return 'email_exist';   //代表存在
            } else {
                return 'email';   //代表bu存在
            }
        } else {
            return false;
        }
    }

    /** 判断指定字段值是否存在
     * @param $field
     * @param $data
     * @return bool  存在true   不存在false
     */
    public static function field_exists($field, $data) {
        $res = self::where($field, $data)->find();
        return $res === null ? false : true;
    }

    public static function register($data, $isphone) {
        $user = self::getUserByName($data['username']);
        $msg = [
            'code' => 400
        ];
        if ($user == null) {
            $user_object = self::create([
                'username' => $data['username'],
                $isphone   => $data['phone_email'],
                'password' => md5($data['password'])
            ]);
            $msg['code'] = 200;
        } else {
            $msg['msg']['exist'] = '用户名已存在！';
        }
        return $msg;
    }
}